package com.wis.service.impl;

import com.wis.entity.TeacherEntity;
import com.wis.mapper.TeacherMapper;
import com.wis.service.TeacherService;
import com.wis.util.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    private TeacherMapper TeacherMapper;
    @Override
    public CommonResult selectTeacherOnPage() {
        List<TeacherEntity> teacherEntities = TeacherMapper.selectTeacherByName(10,1);
        return CommonResult.build(200, "ok", teacherEntities.size(), teacherEntities);
    }

    @Override
    public CommonResult delOneTeacher(Integer id) {
        int i = TeacherMapper.delTeacher(id);
        return i > 0 ? CommonResult.ok() : CommonResult.fail();
    }
    /**
     * 这个是新增
     * **/
    @Override
    public CommonResult showTeacherByName(String name) {

        if(TeacherMapper.isExist(name)!=0)return null;

        TeacherEntity teacher = new TeacherEntity(name,  0, 0);
        // 调用Mapper插入数据
        int i = TeacherMapper.addTeacher(teacher);
        if (i > 0) {  // 插入成功
            return CommonResult.build(200, "ok", i,teacher);
        }
        // 插入失败
        return CommonResult.build(500, "no", i, teacher);
    }

    @Override
    public CommonResult updateTeacher(TeacherEntity teacher) {
        // 调用Mapper更新数据
        int i = TeacherMapper.updateTeacher(teacher);
        if (i > 0) {  // 更新成功
            return CommonResult.build(200, "ok", i,teacher);
        }
        // 更新失败
        return CommonResult.build(500, "no", i, teacher);
    }
}
